package com.kyzh.core.pager.weal.dailytask

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.Base
import com.gushenge.core.beans.DailyTaskBean
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.activities.*
import com.kyzh.core.pager.weal.sign.SignActivity
import com.kyzh.core.databinding.FragmentDailytaskBinding
import com.kyzh.core.databinding.ItemDailyBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.pager.me.user.SafetyActivity
import com.kyzh.core.pager.me.user.UserDataActivity
import com.kyzh.core.pager.weal.rank.RankActivity
import com.kyzh.core.pager.weal.share.ShareNewActivity
import com.kyzh.core.utils.*

class DailyTaskFragment : BaseFragment() {
    private lateinit var  db : FragmentDailytaskBinding
    private val adapter  = Adapter()
    private val adapter1 = Adapter()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db =  FragmentDailytaskBinding.inflate(inflater)
        return  db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.revT.layoutManager = LinearLayoutManager(requireContext())
        db.     revT.adapter =adapter1
        db.rev.layoutManager = LinearLayoutManager(requireContext())
        db.rev.adapter = adapter
    }
    override fun onResume() {
        super.onResume()
        getData()
    }

    fun getData(){
        WealRequest.getDailyTask{
            this?.apply {
                adapter1.setNewInstance(this?.day)
                adapter.setNewInstance(this?.xinshou)
                adapter1.setOnItemClickListener { adapter, _, position ->
                    val dailyTaskItemBean = adapter.data[position] as DailyTaskBean.Item
                    if(dailyTaskItemBean.status == 0) {
                        GoTO(dailyTaskItemBean.type)
                    }else if(dailyTaskItemBean.status == 1){
                        WealRequest.getDailyTaskGift(dailyTaskItemBean.type){
                            getData()
                        }
                    }
                }
                adapter.setOnItemClickListener { adapter, _, position ->
                    val dailyTaskItemBean = adapter.data[position] as DailyTaskBean.Item
                    if(dailyTaskItemBean.status == 0) {
                        GoTO(dailyTaskItemBean.type)
                    }else if(dailyTaskItemBean.status == 1){
                        WealRequest.getDailyTaskGift(dailyTaskItemBean.type){
                            getData()
                        }
                    }
                }
            }

        }
    }

    fun GoTO (type : Int){
        if(startLogin()){
            when(type ){
                2->{
                    startActivity<BrowserActivity>(GlobalKeys.TITLE to getString(R.string.pointLottery),GlobalKeys.LINK to "${Base.BASEURL}/?ct=choujiang&uid=${MMKVConsts.uid}&t=${time}&sign=${httpSign(
                        MMKVConsts.uid
                    )}")
                }
                3->{
                    startActivity<RankActivity>()
                }
                4->{
                    startActivity<BrowserActivity>(GlobalKeys.TITLE to "平台币充值", GlobalKeys.LINK to "${Base.BASEURL}/?ct=h5apppay&uid=${MMKVConsts.uid}")
                }
                5 , 6 ,7->{
                    startActivity<UserDataActivity>()
                }
                8 ,9->{
                    startActivity<SafetyActivity>()
                }
                10->{
                    startActivity<ShareNewActivity>()
                }
                12->{
                    startActivity<SignActivity>()
                }
            }
        }
    }


    private class Adapter :BaseQuickAdapter<DailyTaskBean.Item,BaseDataBindingHolder<ItemDailyBinding>>(
        R.layout.item_daily){
        override fun convert(
            holder: BaseDataBindingHolder<ItemDailyBinding>,
            item: DailyTaskBean.Item
        ) {
            holder.dataBinding?.apply {
                data = item
                when (item.status) {
                    0 -> {
                        if(item.name =="每日签到"){
                            gongneng.text = "签到"
                        }else{
                            gongneng.text = "去完成"
                        }
                        gongneng.setTextColor(Color.WHITE)
                        gongneng.setBackgroundResource(R.drawable.bg_14dp)
                        gongneng.backgroundTintList=    ColorStateList.valueOf(Color.parseColor("#F87706"))

                    }
                    1 -> {
                        gongneng.text = "领取"
                        gongneng.setTextColor(Color.WHITE)
                        gongneng.setBackgroundResource(R.drawable.bg_14dp)
                        gongneng.backgroundTintList=    ColorStateList.valueOf(Color.parseColor("#F87706"))
                    }
                    else -> {
                        if(item.name =="每日签到"){
                            gongneng.text = "已签到"
                        }else{
                            gongneng.text = "已完成"
                        }
                        gongneng.setTextColor(Color.parseColor("#CCCCCC"))
                        gongneng.setBackgroundResource(R.drawable.bg_14dp)
                        gongneng.backgroundTintList=    ColorStateList.valueOf(Color.parseColor("#F7F7F7"))
                    }
                }

            }
        }

    }

}